|
1
|
|
|
var Subject = require('./Subject'), |
|
2
|
|
|
Gender = require('./Gender'), |
|
3
|
|
|
Name = require('./Name'), |
|
4
|
|
|
Fact = require('./Fact'), |
|
5
|
|
|
utils = require('./utils'); |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* A person. |
|
9
|
|
|
* |
|
10
|
|
|
* @constructor |
|
11
|
|
|
* @param {Object} [json] |
|
|
|
|
|
|
12
|
|
|
*/ |
|
13
|
|
|
var Person = function(json){ |
|
14
|
|
|
|
|
15
|
|
|
// Protect against forgetting the new keyword when calling the constructor |
|
16
|
|
|
if(!(this instanceof Person)){ |
|
17
|
|
|
return new Person(json); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
// If the given object is already an instance then just return it. DON'T copy it. |
|
21
|
|
|
if(Person.isInstance(json)){ |
|
22
|
|
|
return json; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
Subject.call(this, json); |
|
26
|
|
|
|
|
27
|
|
|
if(json){ |
|
|
|
|
|
|
28
|
|
|
this.setPrivate(json.private); |
|
29
|
|
|
this.setGender(json.gender); |
|
30
|
|
|
this.setNames(json.names); |
|
31
|
|
|
this.setFacts(json.facts); |
|
|
|
|
|
|
32
|
|
|
} |
|
33
|
|
|
}; |
|
34
|
|
|
|
|
35
|
|
|
Person.prototype = Object.create(Subject.prototype); |
|
36
|
|
|
|
|
37
|
|
|
Person._gedxClass = Person.prototype._gedxClass = 'GedcomX.Person'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Check whether the given object is an instance of this class. |
|
41
|
|
|
* |
|
42
|
|
|
* @param {Object} obj |
|
43
|
|
|
* @returns {Boolean} |
|
44
|
|
|
*/ |
|
45
|
|
|
Person.isInstance = function(obj){ |
|
46
|
|
|
return utils.isInstance(obj, this._gedxClass); |
|
47
|
|
|
}; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Check whether the person is marked as private |
|
51
|
|
|
* |
|
52
|
|
|
* @returns {Boolean} private |
|
53
|
|
|
*/ |
|
54
|
|
|
Person.prototype.isPrivate = function(){ |
|
55
|
|
|
return !!this.private; |
|
56
|
|
|
}; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Set the private flag |
|
60
|
|
|
* |
|
61
|
|
|
* @param {Boolean} isPrivate |
|
62
|
|
|
* @returns {Person} This instance |
|
63
|
|
|
*/ |
|
64
|
|
|
Person.prototype.setPrivate = function(isPrivate){ |
|
65
|
|
|
this.private = isPrivate; |
|
66
|
|
|
return this; |
|
67
|
|
|
}; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get the person's gender |
|
71
|
|
|
* |
|
72
|
|
|
* @returns {Gender} gender |
|
73
|
|
|
*/ |
|
74
|
|
|
Person.prototype.getGender = function(){ |
|
75
|
|
|
return this.gender; |
|
76
|
|
|
}; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Set the person's gender |
|
80
|
|
|
* |
|
81
|
|
|
* @param {Gender} gender |
|
82
|
|
|
* @returns {Person} This instance |
|
83
|
|
|
*/ |
|
84
|
|
|
Person.prototype.setGender = function(gender){ |
|
85
|
|
|
if(gender){ |
|
86
|
|
|
this.gender = Gender(gender); |
|
87
|
|
|
} |
|
88
|
|
|
return this; |
|
89
|
|
|
}; |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get the names |
|
93
|
|
|
* |
|
94
|
|
|
* @return {Name[]} |
|
95
|
|
|
*/ |
|
96
|
|
|
Person.prototype.getNames = function(){ |
|
97
|
|
|
return this.names || []; |
|
98
|
|
|
}; |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Set the names |
|
102
|
|
|
* |
|
103
|
|
|
* @param {Name[]|Object[]} names |
|
104
|
|
|
* @returns {Person} This instance |
|
105
|
|
|
*/ |
|
106
|
|
|
Person.prototype.setNames = function(names){ |
|
107
|
|
|
return this._setArray(names, 'names', 'addName'); |
|
108
|
|
|
}; |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Add a name |
|
112
|
|
|
* |
|
113
|
|
|
* @param {NameForm|Object} name |
|
114
|
|
|
* @returns {Person} This instance |
|
115
|
|
|
*/ |
|
116
|
|
|
Person.prototype.addName = function(name){ |
|
117
|
|
|
return this._arrayPush(name, 'names', Name); |
|
118
|
|
|
}; |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get the facts |
|
122
|
|
|
* |
|
123
|
|
|
* @return {Fact[]} |
|
124
|
|
|
*/ |
|
125
|
|
|
Person.prototype.getFacts = function(){ |
|
126
|
|
|
return this.facts || []; |
|
127
|
|
|
}; |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Set the facts |
|
131
|
|
|
* |
|
132
|
|
|
* @param {Fact[]|Object[]} facts |
|
133
|
|
|
* @returns {Person} This instance |
|
134
|
|
|
*/ |
|
135
|
|
|
Person.prototype.setFacts = function(facts){ |
|
136
|
|
|
return this._setArray(facts, 'facts', 'addFact'); |
|
137
|
|
|
}; |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Add a fact |
|
141
|
|
|
* |
|
142
|
|
|
* @param {Fact|Object} fact |
|
143
|
|
|
* @returns {Person} This instance |
|
144
|
|
|
*/ |
|
145
|
|
|
Person.prototype.addFact = function(fact){ |
|
146
|
|
|
return this._arrayPush(fact, 'facts', Fact); |
|
147
|
|
|
}; |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Export the object as JSON |
|
151
|
|
|
* |
|
152
|
|
|
* @return {Object} JSON object |
|
153
|
|
|
*/ |
|
154
|
|
|
Person.prototype.toJSON = function(){ |
|
155
|
|
|
return this._toJSON(Subject, [ |
|
156
|
|
|
'private', |
|
157
|
|
|
'gender', |
|
158
|
|
|
'names', |
|
159
|
|
|
'facts' |
|
160
|
|
|
]); |
|
161
|
|
|
}; |
|
162
|
|
|
|
|
163
|
|
|
module.exports = Person; |